package {
	import com.PQLoad.PQLoader;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;

	[SWF(width=1920,height=900)]
	public class Main extends Sprite{
		private var p:Player;
		private var map:GameMap;
		public function Main():void{
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		private function init(e:Event):void{
			PQLoader.loadList(["assets/map.jpg","assets/p.png"],{onComplete:show});
		}
		private function show():void{
			map=new GameMap();
			addChild(map);
			map.init(createObstacles(),1920,900,512,512,20);
			p=new Player();
			var img:DisplayObject=PQLoader.getImg("assets/p.png");
			img.x=0-img.width/2;
			img.y=0-img.height;
			p.addChild(img);
			var m:Sprite=new Sprite();
			m.addChild(PQLoader.getImg("assets/map.jpg"));
			map.setMap(m);
			addChild(p);
			p.x=100;
			p.y=100;
			map.addEventListener(MouseEvent.CLICK,onclick);
		}
		private function onclick(e:MouseEvent):void{
			var path:Array=map.findPath(new Point(p.x,p.y),new Point(e.stageX,e.stageY));
			p.move(path);
		}
		/**创建障碍点**/
		private function createObstacles():Array{
			var obs:Array=[];
			var obn:int=0;
			for(var z:int=0;z<512;z++){
				obs[z]=[];
				for(var v:int=0;v<128;v++){
					var ra:int=Math.random()*16;
					if(ra==2){
						obs[z][v]=true;
						obn++;
					}
				}
			}
			trace(obn);
			return obs;
		}
	}
	
}

